package com.alks.common.utils;

import lombok.RequiredArgsConstructor;

import java.lang.reflect.Field;
import java.util.ArrayList;

@RequiredArgsConstructor
public class MysqlUtil<t> {

    /**
     * 自动对实体类中规律命名元素遍历
     * @param repeatName        重复部分
     * @param num               规律性后缀
     */
    public static float fieldOfList(String repeatName,int num,Object entity){
        if (entity==null)
            return 0;
        if (entity.getClass()==ArrayList.class)
            throw new RuntimeException("无法处理List");
        String variableName=repeatName+num;
        try {
            Field field = entity.getClass().getDeclaredField(variableName);
            field.setAccessible(true);
            return (Float)field.get(entity);
        } catch (NoSuchFieldException e) {
            System.err.println("获取对应类对应元素失败，请检查是否存在此元素");
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
}
